<template>
	<view class="topicLearningWrap">
		<view class="topicLearning" v-for="(item, index) in list" :key="item.id" @click="">
			<view class="topicLearning_top">
				<text>{{ item.title }}</text>
			</view>
			<view class="topicLearning_bottom">
				<text class="topicLearning_bottom_left">{{ item.createTime }}</text>
				<text class="topicLearning_bottom_right" @click="FileDownload(item, index)">文件下载</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			list: [], //数据
			ids: '',
			newfilePath: [] //下载数组
		};
	},

	mounted() {
		this.$http({
			url: `topic/list?pageNo=1&pageSize=1000`,
			method: 'POST'
		}).then(res => {
			if (res.code == 200) {
				if (res.data) {
					this.list = res.data.records;
				}
			}
		});
	},
	methods: {
		// 文件下载
		FileDownload(item, index) {
			console.log(item.filePath);

			//文件下载 可以是图片或者文件

			uni.showLoading({
				title: '下载中...'
			});
			uni.downloadFile({
				url: `${this.$localhost}` + 'uploadPath/' + item.filePath,
				success(res) {
					//将文件保存在本地
					uni.saveFile({
						// uni.saveFile({
						tempFilePath: res.tempFilePath, //临时路径
						success: function(res) {
							console.log(res, 'llllllllllllllllllll');
							uni.showToast({
								icon: 'none',
								mask: true,
								title: '文件已保存：' + res.savedFilePath, //保存路径
								duration: 1000
							});
							setTimeout(() => {
								//打开文档查看
								uni.openDocument({
									filePath: res.savedFilePath,
									showMenu: true,
									success: function(res) {
										// console.log('打开文档成功');
									}
								});
							}, 1000);
						}
					});
				},
				fail(err) {
					console.log(err, 'err;;;');
				}
			});
		},

		//保存图片
		save(url) {
			const that = this;
			uni.showLoading({
				title: '正在保存图片...'
			});
			//获取用户的当前设置。获取相册权限
			uni.getSetting({
				success: res => {
					//如果没有相册权限
					if (!res.authSetting['scope.writePhotosAlbum']) {
						//向用户发起授权请求
						uni.authorize({
							scope: 'scope.writePhotosAlbum',
							success: () => {
								//授权成功保存图片到系统相册
								uni.getImageInfo({
									src: url,
									success: function(image) {
										uni.saveImageToPhotosAlbum({
											//图片路径，不支持网络图片路径
											filePath: image.path,
											success: res => {
												uni.hideLoading();
												uni.showToast({
													title: '保存成功！'
												});
												that.isNewGift = false;
											},
											fail: res => {
												console.log(res.errMsg);
												uni.showToast({
													title: res.errMsg
												});
											},
											complete: res => {
												uni.hideLoading();
											}
										});
									}
								});
							},
							//授权失败
							fail: () => {
								uni.hideLoading();
								uni.showModal({
									title: '您已拒绝获取相册权限',
									content: '是否进入权限管理，调整授权？',
									success: res => {
										if (res.confirm) {
											//调起客户端小程序设置界面，返回用户设置的操作结果。（重新让用户授权）
											uni.openSetting({
												success: res => {
													console.log(res.authSetting);
												}
											});
										} else if (res.cancel) {
											uni.showToast({
												title: '已取消！',
												icon: 'none'
											});
										}
									}
								});
							}
						});
					} else {
						uni.getImageInfo({
							src: url,
							success: function(image) {
								//如果已有相册权限，直接保存图片到系统相册
								uni.saveImageToPhotosAlbum({
									filePath: image.path,
									success: res => {
										uni.hideLoading();
										uni.showToast({
											title: '保存成功！'
										});
										that.isNewGift = false;
									},
									fail: res => {
										uni.hideLoading();
										console.log(res.errMsg);
										uni.showToast({
											title: res.errMsg,
											icon: 'none'
										});
									},
									//无论成功失败都走的回调
									complete: res => {
										uni.hideLoading();
									}
								});
							}
						});
					}
				},
				fail: res => {
					uni.showToast({
						title: res.msg
					});
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.topicLearningWrap {
	padding: 32rpx;
	// background: #fff;

	// 专题学习
	.topicLearning {
		width: 100%;
		height: 190rpx;
		// margin-left: 20rpx;

		// background: red;
		box-shadow: 0rpx 10rpx 20rpx 0rpx rgba(29, 82, 130, 0.06);
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-top: 30rpx;

		.topicLearning_top {
			padding-left: 25rpx;
			width: 100%;
			height: 96rpx;
			font-size: 32rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 700;
			color: #2a3447;
			line-height: 48rpx;
		}

		.topicLearning_bottom {
			padding-left: 25rpx;
			display: flex;
			justify-content: space-between;

			.topicLearning_bottom_right {
				margin-right: 30rpx;
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #3b72ec;
				line-height: 24rpx;
			}
		}
	}
}
</style>
